﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Company;
using System.Web.Security;

public partial class Company_Infomation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Membership.GetUser() != null)
            {
                var _i = CompanyInformationManager.GetCompanyInformation(Membership.GetUser().UserName);
                if (_i != null)
                {
                    ViewPanel.Visible = true;
                    UpdatePanel.Visible = false;

                    lbl_CompanyName.Text = _i.CompanyName;
                    lbl_Nature.Text = CompanyNatureManager.GetCompanyNature(Convert.ToInt32(_i.Nature)).Nature;
                    lbl_Industry.Text = IndustryManager.GetIndustry(Convert.ToInt32(_i.Industry)).Name;
                    lbl_Location.Text = LocationManager.Get(Convert.ToInt32(_i.Location)).Name;
                    lbl_RegisteredCapital.Text = _i.RegisteredCapital;
                    lbl_CompanySize.Text = CompanySizeManager.GetCompanySize(Convert.ToInt32(_i.Size)).Size;
                    lbl_DateOfEstablishment.Text = string.IsNullOrEmpty(_i.DateOfEstablishment.ToString()) ? "--" : Convert.ToDateTime(_i.DateOfEstablishment).ToShortDateString();
                    lbl_Description.Text = _i.Description;
                }
                else
                {
                    ViewPanel.Visible = false;
                    UpdatePanel.Visible = true;
                }
            }
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            CompanyInformationManager.SaveCompanyInformation(
                Membership.GetUser().UserName,
                txt_CompanyName.Text,
                Convert.ToInt32(ddl_CompanySize.SelectedValue),
                Convert.ToInt32(ddl_CompanyNature.SelectedValue),
                Convert.ToInt32(ddl_Industry.SelectedValue),
                Location1.City,
                string.IsNullOrEmpty(txt_DateOfEstablishment.Text) ? DateTime.MinValue : Convert.ToDateTime(txt_DateOfEstablishment.Text),
                txt_Description.Text,
                txt_RegisteredCapital.Text);

            ViewPanel.Visible = true;
            UpdatePanel.Visible = false;

            Response.Redirect(Request.Url.ToString());

        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        ViewPanel.Visible = true;
        UpdatePanel.Visible = false;
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            CompanyInformationManager.Delete(Membership.GetUser().UserName);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        ViewPanel.Visible = false;
        UpdatePanel.Visible = true;

        var _i = CompanyInformationManager.GetCompanyInformation(Membership.GetUser().UserName);
        if (_i != null)
        {
            txt_CompanyName.Text = _i.CompanyName;
            ddl_CompanyNature.SelectedValue = _i.Nature.ToString();
            ddl_Industry.SelectedValue = _i.Industry.ToString();
            //s_from = drow0["qyfrom"].ToString() + "," + drow0["qyfromid"].ToString();
            //mapkey_Text.Value = drow0["mapKey"].ToString();
            txt_RegisteredCapital.Text = _i.RegisteredCapital;
            ddl_CompanySize.SelectedValue = _i.Size.ToString();
            txt_DateOfEstablishment.Text = _i.DateOfEstablishment.ToString();
            txt_Description.Text = _i.Description;
        }
    }
}
